package com.sanpu.kxe.common.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sanpu.kxe.R;

/**
 * Description：两按钮垂直布局的弹框
 * Created by 薛勇军 on 2019/5/16.
 */

public class CommonVerticalDialog extends Dialog {

    private Context mContext;

    // 编辑标题
    private TextView titleTv, contentTv, remarkTv;

    // 确定
    public TextView confirmTv;

    // 取消
    public TextView cancelTv;

    public DialogViews_ask mAction_ask;

    public interface DialogViews_ask {
        void doOk();

        void doCancle();

        class DialogViews_askImpl implements DialogViews_ask {

            @Override
            public void doOk() {

            }

            public void doOk(String text) {

            }

            @Override
            public void doCancle() {

            }
        }
    }

    /**
     * 实例化
     */
    private void initView() {
        // 编辑标题
        titleTv = (TextView) findViewById(R.id.tv_dialog_title);
        contentTv = (TextView) findViewById(R.id.contentTv);
        remarkTv = (TextView) findViewById(R.id.remarkTv);
        confirmTv = (TextView) findViewById(R.id.tv_dialog_confirm);
        cancelTv = (TextView) findViewById(R.id.tv_dialog_cancle);
    }

    /**
     * 内容一行
     */
    public CommonVerticalDialog(Context context, String name, String message, DialogViews_ask listener) {
        super(context, R.style.dialog_normal);
        createDialog(context, name, message, null, listener);
    }

    /**
     * 内容两行，间隔12dp
     */
    public CommonVerticalDialog(Context context, String name, String message, String remark, DialogViews_ask listener) {
        super(context, R.style.dialog_normal);
        createDialog(context, name, message, remark, listener);
    }


    /**
     * 创建具体dialog实例
     *
     * @param context       上下文
     * @param listener      回调监听
     */
    private void createDialog(Context context, String name, String message, String remark, DialogViews_ask listener) {
        this.mContext = context;
        setContentView(R.layout.common_vertical_dialog);
        this.mAction_ask = listener;
        initView();
        if (!TextUtils.isEmpty(name)) {
            titleTv.setText(name);
        }
        if (!TextUtils.isEmpty(message)) {
            contentTv.setText(message);
        }
        if (!TextUtils.isEmpty(remark)) {
            remarkTv.setVisibility(View.VISIBLE);
            remarkTv.setText(remark);
        }

        // 数据填充
        setDataAndListener();
    }

    /**
     * 数据填充和添加监听
     */
    private void setDataAndListener() {
        // 确定
        confirmTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
                if (mAction_ask != null) {
                    mAction_ask.doOk();
                }
            }
        });
        // 取消
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
                if (mAction_ask != null) {
                    mAction_ask.doCancle();
                }
            }
        });
    }

    /**
     * 关闭dialog
     */
    public void close() {
        if (!((Activity) mContext).isFinishing()) {
            ((Activity) mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (isShowing()) {
                        dismiss();
                    }
                }
            });
        }
    }
}
